<template>
  <div class="container box">
    <div class="search_form">
      <div>
        <el-input size="mini" clearable placeholder="新闻标题" v-model="searchForm.title" class="others"></el-input>
        <el-button class="search_btn" type="primary" icon="el-icon-search" size="mini" @click=checkNewList>查询</el-button>
        <el-button icon="el-icon-refresh-right" size="mini" @click="resetSearch">重置</el-button>
      </div>
      <!-- <div>
        <el-tooltip class="item" content="新增" placement="top">
          <el-button size="mini" icon="iconfont icon-tianjia"></el-button>
        </el-tooltip>
      </div> -->
    </div>
    <keep-alive>
      <BaseTable :loading="loading" ref="baseTable" :data="tableData" :columns="columns" :tableHeight="tableHeight" :pagination="queryall" @details="getSelectRow" @size-change="sizeChange" @current-change="pageChange"></BaseTable>
    </keep-alive>
  </div>
</template>
<script>
import drawer from '@/components/BaseTable/drawer'
import BaseTable from '@/components/BaseTable'
export default {
  components: {
    drawer,
    BaseTable,
  },
  name: 'outServiceManageList',
  data() {
    return {
      tableHeight: window.innerHeight - 200,
      queryall: {
        totalNum: 10,
        startpage: 1,
        pagesize: 15
      },
      searchForm: {
        title: ''
      },
      workComps: [],
      tableData: [],
      loading: false,
      columns: [
        {
          prop: 'title',
          label: '标题 '
        }, {
          prop: 'label',
          label: '标签 '
        }, {
          prop: 'type',
          label: '分类'
        }, {
          prop: 'createName',
          label: '创建人'
        }, {
          prop: 'createTime',
          label: '创建时间'
        }, {
          label: '操作',
          render: (h, ctx) => {
            return h('el-button', {
              props: {
                size: 'small',
                type:'danger '
              },
              on: {
                click: () => {
                  this.delNewRow(ctx.row.id)
                }
              }
            }, '删 除')
          }
        }
      ],
    }
  },
  created() {
  },
  async activated() {
    var menuArr = []
    var menuId = ""
    if(localStorage.getItem("menuArr")){
      menuArr = JSON.parse(localStorage.getItem("menuArr"))
    }
    menuArr.map(item=>{
      item.children.map(v=>{
        if(window.location.href.includes(v.url)){
          menuId = v.id
        }
      })
    })

    if(menuId){
      var menuData = []
      await this.$http.get(`/${this.$ServiceName}/api/external/sysmenubtn/listresourcebtnbyid?menuId=${menuId}`,{ timeout: 30000,})
              .then(res => {
                res.data.data.map(item=>{
                  menuData.push(item.resourceName)
                })

          if(!menuData.includes("新闻管理删除")){
            this.columns.map((item,index)=>{
              if(item.label == "操作"){
                this.columns.pop()
              }
            })
          }

        }).catch((error)=>{})
    }
    if(this.$store.state.app.searchResouce =="menu"){
        this.getNewsList()
    }
  },
    methods: {
    //删除新闻
    delNewRow(id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        // type: 'warning'
      }).then(() => {
        let params = { id: id }
        this.$http.delete(`/psys-external-service/api/external/exnewsinfo/${this.$version}/deletebyid`, {
          params: params
        }).then(res => {
          if (res.data.success) {
            this.$message({
              type: 'info',
              message: '删除成功'
            });
            this.checkNewList()
          } else {
            this.$message({
              type: 'error',
              message: '删除失败'
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
    //查询新闻
    checkNewList() {
      let params = { title: this.searchForm.title }
      let para = Object.assign({}, this.queryall, params)
      this.$http.get(`/psys-external-service/api/external/exnewsinfo/${this.$version}/listall`, {
        params: para
      }).then(res => {
        this.tableData = res.data.data.pages
        this.queryall.pagesize = res.data.data.pageSize
        this.queryall.totalNum = res.data.data.totalNum
      })
    },
    getNewsList() {
      var params = {
      }
      let para = Object.assign({}, this.queryall, params)
      this.$http.get(`/psys-external-service/api/external/exnewsinfo/${this.$version}/listall`, {
        params: para
      }).then(res => {
        this.tableData = res.data.data.pages
        this.queryall.pagesize = res.data.data.pageSize
        this.queryall.totalNum = res.data.data.totalNum
      })
    },
    getSelectRow(rowData) {
      this.selectBrageData = rowData;
      //console.log(rowData)
    },
    pageChange(val) {
      this.queryall.startpage = val
      this.getNewsList()
    },
    sizeChange(val) {
      this.queryall.pagesize = val
      this.getNewsList()
    },
    resetSearch() {
      this.searchForm = {
        title: null,
      }
      this.queryall.startpage = 1
    },
  },
}
</script>
<style lang="less" scoped>
@import "./../../../assets/searchfrom";
/deep/.ql-container {
  height: 380px;
}
</style>
